import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import LoginRoutes from "@/views/logins/route"
import errorRoutes from "@/views/errorpage/route"
import mainRoutes from "@/views/mains/route"


const routes = [
    {
      path:"/",
      redirect:'/login'
    },
    {
      path:"/demo",
      name:"demo",
      component:()=>import("@/views/Demo.vue")
    },
    ...LoginRoutes,
    ...errorRoutes,
    ...mainRoutes,
    {
      path:"/:pathMatch(.*)",
      redirect:{
        name:"notfound"
      }
    }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

// router.beforeEach((to,from)=>{}),next是可选参数，可写可不写，return false是取消导航，如果返回值为true或者是undefined意味着通过验证

router.beforeEach((to,from)=>{
    if(to.name=="login"|| to.name == "badservice"|| to.name == "notfound"){
      return true;
    }else{
      var token = sessionStorage.getItem("authtoken");
      if(token){
        return true
      }else{
        return false;
      }
    }
})

export default router

